﻿#include "snap.h"

Snap::Snap(QObject *parent)
    : Operation{parent}
    , m_nImageNum(1)
    //, m_pDescription((Description*)parent)
{}

int Snap::nImageNum() const
{
    return m_nImageNum;
}

void Snap::setNImageNum(int newNImageNum)
{
    m_nImageNum = newNImageNum;
}

void Snap::run(){
    //Device* pDevice = m_pDescription->FindDeviceById(m_strDeviceID);
    QMap<QString,QVariant> map;
    map.insert("classname",QVariant(this->getClassName()));
    map.insert("content",QVariant(m_nImageNum));
    m_pDevice->writeData(QVariant::fromValue(map));
}

QString Snap::getClassName(){
    return this->metaObject()->className();
}

void Snap::writeXML(QXmlStreamWriter& writer){
    // 写入当前操作的信息
    writer.writeStartElement("Snap");
    writer.writeAttribute("device", m_strDeviceID);

    writer.writeTextElement("image", QString::number(m_nImageNum));

    writer.writeEndElement();  // 结束当前操作的元素
}

void Snap::readFromXML(QXmlStreamReader& reader){
    QXmlStreamAttributes attributes = reader.attributes();
    if(attributes.hasAttribute("device")) {
        m_strDeviceID = attributes.value("device").toString();
    }

    QXmlStreamReader::TokenType token = reader.readNext();
    while (!(reader.tokenType() == QXmlStreamReader::EndElement &&
             reader.name() == QStringLiteral("Snap"))) {

        if (reader.name() == QStringLiteral("image")) {
            m_nImageNum = reader.readElementText().toInt();
        }

        reader.readNext();
    }
}
